package com.gzist.blog_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzist.blog_system.domain.User;
import com.gzist.blog_system.domain.result.Result;
import com.gzist.blog_system.domain.vo.LoginParams;
import com.gzist.blog_system.domain.vo.UserInfoParams;
import com.gzist.blog_system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.intellij.lang.annotations.Pattern;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理模块")
public class UserController {

    @Resource
    private UserService userService;

    /*
     * @description:
     *  登录接口
     **/
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result login(@ApiParam("登录参数") @RequestBody LoginParams loginParams) {
        log.info("==》{}", loginParams);
        String token = userService.login(loginParams);
        log.info("登录操作==》{}", token);
        return Result.success(token);
    }

    /*
     * @description:
     *  注册接口
     **/
    @ApiOperation("注册接口")
    @PostMapping("/register")
    public Result register(String username, String password) {
        log.info("账号密码：{}",username+"  "+password);
        //首先进行注册账号的查询，不存在才进行注册，否则注册失败
        User u = userService.getOne(new QueryWrapper<User>().eq("username",username));
        if (u == null) {
            userService.register(username, password);
            return Result.success();
        } else {
            return Result.error("该账号已存在！");
        }
    }

    /*
     * @description:
     *  获取用户列表
     * @author: 黄泽建
     **/
    @GetMapping
    @ApiOperation("获取用户列表")
    public Result list() {
        List<User> list = userService.list();
        log.info("获取到的数据====>{}", list);
        return Result.success(list);
    }

    /*
     * @description:
     *  获取个人用户信息
     * @author: 黄泽建
     **/
    @GetMapping("/userInfo")
    @ApiOperation("获取用户信息")
    public Result userInfo() {
        UserInfoParams list = userService.userInfo();
        log.info("获取到的数据====>{}", list);
        return Result.success(list);
    }

    /*
     * @description:
     *  获取用户管理身份密钥
     * @author: 黄泽建
     **/
    @GetMapping("/key")
    @ApiOperation("获取用户管理身份密钥")
    public Result userKey(@ApiParam("令牌token") @RequestHeader("Authorization") String token) {
        String key = userService.userKey(token);
        log.info("获取用户管理身份密钥====>{}", key);
        return Result.success(key);
    }
    /*
     * @description:
     *  更改用户信息
     * @author: 黄泽建
     **/
    @PostMapping("/update")
    @ApiOperation("更改用户信息")
    public Result updateUserInfo(@RequestBody @ApiParam("用户信息") UserInfoParams userInfoParams) {
        boolean result = userService.updateUserInfo(userInfoParams);
        log.info("获取到的数据====>{}", result);
        if (!result) return Result.error("更改失败");
        return Result.success();
    }

    /*
     * @description:
     *  更改用户头像
     * @author: 黄泽建
     **/
    @PostMapping("/updateAvatar")
    @ApiOperation("更改用户头像")
    public Result updateAvatar(@ApiParam("用户头像") @RequestParam String userPic) {
        boolean result = userService.updateAvatar(userPic);
        log.info("获取到的数据====>{}", result);
        if (!result) return Result.error("更改失败");
        return Result.success();
    }

    /*
     * @description:
     *  更改用户密码
     * @author: 黄泽建
     **/
    @PostMapping("/updatePwd")
    @ApiOperation("更改用户密码")
    public Result updatePwd(@RequestBody Map<String, String> params, @RequestHeader("Authorization") String token) {
        String result = userService.updatePwd(params,token);
        if (!result.equals("success")) return Result.error(result);
        return Result.success();
    }
}
